package com.example.sample.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.view.View.OnClickListener;
import android.widget.Toast;


import com.example.app.BaseActivity;
import com.example.sample.R;

import net.liteheaven.xblib.widget.Toast.SmartToast;
import net.liteheaven.xblib.widget.Toast.ToastFactory;

import java.io.File;


public class FileBrowseActivity extends BaseActivity {

    private FileListAdapter mAdapter;
    private ListView mFileList;
    private Button mBtnUp;
    private View mContainer;
    private TextView mCurrentPath;
    private File mCurFile = new File("/");
    private File [] mCurFileList = null;

    public class ItemFileList extends LinearLayout{

        private ImageView ivImage;
        private TextView tvFile;
        private File mFile;

        public ItemFileList(Context context) {
            super(context);
            init();
        }

        public ItemFileList(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }

        public ItemFileList(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init();
        }

        private void init(){
            LayoutInflater li = getLayoutInflater();
            li.inflate(R.layout.item_file_browse_list, this);
            ivImage = (ImageView)findViewById(R.id.ivImage);
            tvFile = (TextView)findViewById(R.id.tvFile);
            mContainer = findViewById(R.id.container);
            mContainer.setOnClickListener(getDefaultOnClickListener());
        }

        public void setName(String name){
            tvFile.setText(name);
        }

        public void setImage(int resId){
            ivImage.setImageResource(resId);
        }

        private OnClickListener getDefaultOnClickListener(){
            return new OnClickListener(){

                @Override
                public void onClick(View v) {

                    if (mFile.isDirectory()){
                        updateList(mFile);
                    }else{
                        SmartToast.cancelLastToast();
                        ToastFactory.makeText(FileBrowseActivity.this, "不是文件夹", Toast.LENGTH_SHORT, 1).show();
                    }
                }
            };
        }

        public void setFile(File f){
            mFile = f;
            String fileName = "??";


            if (f != null){
                fileName = f.getName();

                if (f.isDirectory()){
                    setImage(R.drawable.ic_folder);
                }else{
                    setImage(R.drawable.ic_file);
                }

            }
            setName(fileName);
        }
    }

    public class FileListAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            int count = 0;
            if (mCurFileList != null){
                count = mCurFileList.length;
            }
            return count;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ItemFileList ifl = null;
            if (convertView == null){
                ifl = new ItemFileList(FileBrowseActivity.this);
            }else{
                ifl = (ItemFileList)convertView;
            }
            File f = mCurFileList[position];
            ifl.setFile(f);
            return ifl;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file_browse);
        mFileList = (ListView)findViewById(R.id.fileList);
        mCurrentPath  =(TextView)findViewById(R.id.tvCurrentPath);

        mAdapter = new FileListAdapter();
        mFileList.setAdapter(mAdapter);
        updateList(Environment.getExternalStorageDirectory());

        mBtnUp = (Button)findViewById(R.id.btnUp);
        mBtnUp.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                File parent = mCurFile.getParentFile();
                if (parent != null){
                    updateList(parent);
                }
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_file_browse, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handleEvt clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private File [] getFileList(){
        File [] fileList = null;
        if (mCurFile!=null){
            fileList = mCurFile.listFiles();
        }
        return fileList;
    }

    private void updateList(File f){
        if (f != null){
            mCurFile = f;
            if (mAdapter!=null){
                mAdapter.notifyDataSetChanged();
            }
            mCurrentPath.setText(f.getAbsolutePath());

            mCurFileList = getFileList();
        }
    }
}
